var express = require("express");
var mysql = require('mysql');
var app = express();
var bodyParser = require("body-parser"); // post需要
var url=require('url')
var querystring = require("querystring");
app.use(bodyParser.urlencoded({ extended: true }));

// 跨域
app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", ' 3.2.1');
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});
// 配置数据库信息
var connection = mysql.createConnection({
  host: 'localhost', // 主机地址
  // port: '3306', // 端口
  user: 'root', // 用户名
  password: '123456', // 密码
  database: 'training' // 数据库名
});

// 连接
connection.connect();
app.get("/", function (req, res) {
    // console.log(req.query);
    var sql = 'select * from cerTime';
    connection.query(sql , function (err, data) {
      if (err) {
        console.log(err);
      } else {
        var result = {
          "status": "200",
          "message": "success",
        }
        result.data = data;

        res.end(JSON.stringify(result));
      }
    });
});
app.get("/a", function (req, res) {
  // console.log(req.query);
  var sql = 'select * from studydata';
  connection.query(sql , function (err, data) {
    if (err) {
      console.log(err);
    } else {
      var result = {
        "status": "200",
        "message": "success",
      }
      result.data = data;

      res.end(JSON.stringify(result));
    }
  });
});
app.get("/b", function (req, res) {
  // console.log(req.query);
  var sql = 'select * from study';
  connection.query(sql , function (err, data) {
    if (err) {
      console.log(err);
    } else {
      var result = {
        "status": "200",
        "message": "success",
      }
      result.data = data;

      res.end(JSON.stringify(result));
    }
  })
})
app.post('/c',function(req,res){
  var params=[req.body.user]
  var sql="INSERT INTO user(Uname) VALUES(?)"
  connection.query(sql, params, function (err, data) {
    if (err) {
      res.end('error')
    } else {
      var result = {
        "status": "200",
        "message": 'success',
      }
      res.end(JSON.stringify(result))
    }
  })
})
app.post('/d',function (req,res) {
  var params=[parseInt(req.body.hour*60*60)+parseInt(req.body.minute*60)+parseInt(req.body.second),req.body.time]
  var sql="INSERT INTO study(studyTime,studyDate) VALUES(?,?)"
  connection.query(sql, params, function (err, data) {
    if (err) {
      res.end('error')
    } else {
      var result = {
        "status": "200",
        "message": 'success',
      }
      res.end(JSON.stringify(result))
    }
  })  
})
app.post('/e',function (req,res) {

  var params=[req.body.content,req.body.id,req.body.img,req.body.postUname,req.body.postUimg]
  console.log(params)
  var sql="INSERT INTO post(postContent,Uid,postImg,postUname,postUimg) VALUES(?,?,?,?,?)"
  connection.query(sql, params, function (err, data) {
    if (err) {
      res.end('error')
    } else {
      var result = {
        "status": "200",
        "message": 'success',
      }
      res.end(JSON.stringify(result))
    }
  })  
})
app.get('/f',function (req,res) {
  var sql = 'select * from post';
  connection.query(sql , function (err, data) {
    if (err) {
      console.log(err);
    } else {
      var result = {
        "status": "200",
        "message": "success",
      }
      result.data = data;
      res.end(JSON.stringify(result));
    }
})
})
app.post('/g',function (req,res) {
  var params=[req.body.content,req.body.img,req.body.name,req.body.id]
  console.log(params)
  var sql="UPDATE post SET postTalkContent=?,postTalkImg=?,postTalkName=?,postTalk=postTalk+1 WHERE postId=?"
  connection.query(sql, params, function (err, data) {
    if (err) {
      res.end('error')
    } else {
      var result = {
        "status": "200",
        "message": 'success',
      }
      res.end(JSON.stringify(result))
    }
  }) 
})
app.get('/h',function(req,res){
  var sql='select * from post where postId=?';
  var which=req.url.slice(3,)
  connection.query(sql ,which, function (err, data) {
    if (err) {
      console.log(err);
    } else {
      var result = {
        "status": "200",
        "message": "success",
      }
      result.data = data;
      res.end(JSON.stringify(result));
      console.log(result)
    }
})
})
app.post('/i',function(req,res){
  var params=[req.body.id]
  var sql='update post set postLike=postLike+1 where postId=?'
  connection.query(sql, params, function (err, data) {
    if (err) {
      res.end('error')
    } else {
      var result = {
        "status": "200",
        "message": 'success',
      }
      res.end(JSON.stringify(result))
    }
  })
})
app.post('/j',function(req,res){
  var params=[req.body.id]
  var sql='update post set postCollect=postCollect+1 where postId=?'
  connection.query(sql, params, function (err, data) {
    if (err) {
      res.end('error')
    } else {
      var result = {
        "status": "200",
        "message": 'success',
      }
      res.end(JSON.stringify(result))
    }
  })
})
app.get("/k", function (req, res) {
  // console.log(req.query);
  var sql = 'select sum(studyTime)as totalTime from study';
  connection.query(sql , function (err, data) {
    if (err) {
      console.log(err);
    } else {
      var result = {
        "status": "200",
        "message": "success",
      }
      result.data = data;

      res.end(JSON.stringify(result));
    }
  })
})
app.listen(8080);
console.log('8080 running');